package cn.com.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

public class Utils {
	/** 接口返回信息设置 */
	public static final String RESULT = "error";

	public static final String MESSAGE = "information";

	public static final String DATAS = "datas";

	public static final String Page = "page";

	/** 路径 */
	public static final String SYS_VIEWS = "views/";

	public static final String SYS_API = "api/";

	public static final String SYS_BACKSTAGE = "backstage/";

	public static final String SYS_USER = "user/";

	public static final String SYS_SELLER = "seller/";

	/** 菜单级别 */
	public static final String TYPE_LEVEL = "type_level";

	/** 正常返回 */
	public static final String RESULT_RC_SUCCESS = "0000";

	/** 参数异常 */
	public static final String RESULT_RC_PARAMTER_ERROR = "0004";

	/** 已存在 */
	public static final String RESULT_RC_EXIST = "0003";

	/** 正常返回没有查到数据 */
	public static final String RESULT_RC_NO_DATA = "1111";

	/** 系统异常 */
	public static final String RESULT_RC_EXCEPTION = "-9999";

	/** 校验失败 */
	public static final String RESULT_RC_CHECK_FAIL = "0002";

	/** 默认分页大小 */
	public static final int DEFAULT_PAGE_COUNT = 20;

	/** 单次查询返回最大记录条数 */
	public static final int DEFAULT_PAGE_TOTAL_COUNT = 50;

	/**
	 * 产生6为随机数方法 NextInt(final int min, final int max)
	 * 
	 * @return 返回随机数
	 */
	public static int sixRandomNum() {
		final int min = 100000;
		final int max = 999999;
		Random rand = new Random();
		int tmp = Math.abs(rand.nextInt());
		return tmp % (max - min + 1) + min;
	}

	/**
	 * 产生4位随机数方法 NextInt(final int min, final int max)
	 * 
	 * @return 返回随机数
	 */
	public static int fourRandomNum() {
		final int min = 1000;
		final int max = 9999;
		Random rand = new Random();
		int tmp = Math.abs(rand.nextInt());
		return tmp % (max - min + 1) + min;
	}

	/**
	 * 验证字符中否有空格
	 * 
	 * @param str
	 * @return
	 */
	public static boolean checkTrim(String str) {
		String rea = "^[^\\s]*";
		Pattern p = Pattern.compile(rea);
		Matcher m = p.matcher(str);
		return m.matches();
	}

	/**
	 * 自定义随机数方法
	 * 
	 * @return 返回对应位数的随机数
	 */
	public static String customNum(int minVal) {
		final int min = minVal;
		final int max = minVal * 10 - 1;
		Random rand = new Random();
		int tmp = Math.abs(rand.nextInt());
		return String.valueOf(tmp % (max - min + 1) + min);
	}

	public static int customNumTo(int maxVal) {
		int max = maxVal - 1;
		int min = 0;
		Random random = new Random();
		int s = random.nextInt(max) % (max - min + 1) + min;
		return s;
	}

	/**
	 * 获取用户登录token值
	 * 
	 * @param customerId
	 *            用户编号
	 * @return token值
	 */
	public static String getToken(String userId) {
		String s = UUID.randomUUID().toString();
		return s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18) + s.substring(19, 23) + userId
				+ s.substring(24);
	}

	/**
	 * orderId:最大订单号 usercode:用户ID 生成订单号
	 * 
	 * @return
	 */
	public static String getDingDnaNo(Integer orderId, String usercode) {
		// 生成订单号
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
		String date = sdf.format(new Date());
		String prexf = String.format("%05d", orderId);
		int length = usercode.length();
		String user = usercode.substring(length - 4, length);
		String orderNo = String.format("H%s%s", date, prexf);
		return orderNo;
	}

	/**
	 * @Title: getDingDanNo
	 * @Description: 获取某城市的订单编号
	 * @param cityid
	 * @return
	 * @return String
	 */
	public static String getDingDanNo(String cityid) {
		// 生成订单号
		String uniqueString = UniqueStringGenerator.getUniqueString();
		return uniqueString;
	}

	/**
	 * 订单号生产规则-----时间+5位数字(每天从0开始)
	 * 
	 * @return
	 */
	public static String getDingDanTitles() {
		String titles = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
		String date = sdf.format(new Date());

		return titles;
	}

	/**
	 * 获取资源消息
	 * 
	 * @param request
	 * @param code
	 *            提示信息编号
	 * @param params
	 *            提示信息替换参数
	 * @return 提示信息
	 */
	public static String getMessage(HttpServletRequest request, String code, String[] params) {

		String language = "0";
		if (code == null || code.length() <= 0) {
			return "";
		}
		String message = null; // StartOnLoadService.languageMap.get(code.toUpperCase()
								// + "_" + language);
		if (message == null) {
			message = "";
		}

		if (params != null && message != null && message.length() > 0) {
			for (int i = 0; i < params.length; i++) {
				String key = "{" + i + "}";
				message = message.replace(key, params[i]);
			}
		}
		return message;
	}

	private static double EARTH_RADIUS = 6378.137;

	private static double rad(double d) {
		return d * Math.PI / 180.0;
	}

	public static double GetDistance(double lat1, double lng1, double lat2, double lng2) {
		double radLat1 = rad(lat1);
		double radLat2 = rad(lat2);
		double a = radLat1 - radLat2;
		double b = rad(lng1) - rad(lng2);
		double s = 2 * Math.asin(Math.sqrt(
				Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
		s = s * EARTH_RADIUS;
		// System.out.println("distance-s-"+s);
		// s = Math.round(s * 10000) / 10000;
		return s;
	}

	public static double GetDistance(String lat1, String lng1, String lat2, String lng2) {
		double s = 0.0;
		try {
			if (!Util.isNull(lat1) && !Util.isNull(lng1) && !Util.isNull(lat2) && !Util.isNull(lng2)) {
				double lat11 = Double.parseDouble(lat1);
				double lng11 = Double.parseDouble(lng1);
				double lat22 = Double.parseDouble(lat2);
				double lng22 = Double.parseDouble(lng2);
				s = GetDistance(lat11, lng11, lat22, lng22);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return s;
	}

	public static String arrayToStr(String[] str, String joinstr) {
		String result = "";
		if (str != null && str.length > 0) {
			for (int i = 0; i < str.length; i++) {
				if (!"".equals(str[i])) {
					if (i != 0) {
						result += joinstr;
					}
					result += str[i];
				}
			}
		}
		return result;
	}

}
